package com.java.controller;

import com.github.pagehelper.PageInfo;
import com.java.bean.Bill;
import com.java.bean.BillType;
import com.java.sevice.BillService;
import com.java.sevice.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: YNB
 * @Description:
 * @Date Created in 2021-01-02 9:38
 * @Modified By:
 */
@Controller
//@RequestMapping("/bill")
class BillController {
    @Resource
    private TypeService typeService;
    @Resource
    private BillService billService;

    /*** 查询
     * @param  b
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Bill b, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        List<Bill> list = billService.list(b);
        model.addAttribute("list", list);
        model.addAttribute("bill", b);
        return "/bill/list";
    }

    /***
     * 调到添加页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        return "/bill/add";
    }

    /***
     * 添加
     * @param b
     * @return
     */
    @RequestMapping("/add")
    public String add(Bill b) {
        billService.add(b);
        return "redirect:/bill/list";
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        billService.delete(id);
        return "redirect:/bill/list";
    }

    /***
     * 修改
     * @param id
     * @return
     */
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Long id, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        Bill bill = billService.get(id);
        model.addAttribute("bill", bill);
        return "/bill/update";
    }

    /***
     * 修改
     * @param b
     * @return
     */
    @RequestMapping("/update")
    public String update(Bill b) {
        billService.update(b);
        return "redirect:/list-page";
    }

    /*** 查询
     * @param b
     * @param model
     * @return */
    @RequestMapping("/list-page")
    public String listPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Bill b, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        PageInfo<Bill> pageInfo = billService.listPage(b, pageNum, pageSize);
        model.addAttribute("page", pageInfo);
        model.addAttribute("bill", b);
        System.out.println(model);
        System.out.println(pageInfo);
        return "bill/list-page";
    }
}